//
//  HomeHeaderView.m
//  quanminzhekou
//
//  Created by zxl on 15/1/7.
//  Copyright (c) 2015年 zhongqinglongtu. All rights reserved.
//

#import "HomeHeaderView.h"

@implementation HomeHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        CGFloat modulus = CGRectGetWidth(frame) / 320.0;
        
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(frame), modulus * 128.5)];
        _scrollView.pagingEnabled = YES;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.scrollsToTop = NO;
        _scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [self addSubview:_scrollView];
        
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(_scrollView.frame) - 13 - 10, CGRectGetWidth(frame), 13)];
        [_pageControl addTarget:self action:@selector(onTapPageControl:) forControlEvents:UIControlEventValueChanged];
        [self addSubview:_pageControl];
        
        CGRect iconFrame = CGRectMake(CGRectGetWidth(frame) / 8 - 45 * modulus / 2.0, CGRectGetMaxY(_scrollView.frame) + 4 * modulus, 45 * modulus, 45 * modulus);
        CGFloat offset = CGRectGetWidth(frame) / 4;
        
        _firstBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _firstBtn.tag = 0;
        _firstBtn.exclusiveTouch = YES;
        _firstBtn.frame = CGRectOffset(iconFrame, offset * 0, 0);
        [_firstBtn setImage:[UIImage imageNamed:@"icon_nvzhuang"] forState:UIControlStateNormal];
        [_firstBtn addTarget:self action:@selector(onTapBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_firstBtn];
        
        UILabel* firstLab = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(_firstBtn.frame), CGRectGetMaxY(_firstBtn.frame), CGRectGetWidth(iconFrame), 10)];
        firstLab.backgroundColor = [UIColor clearColor];
        firstLab.text = @"女装";
        firstLab.textAlignment = NSTextAlignmentCenter;
        firstLab.font = [UIFont systemFontOfSize:12];
        firstLab.textColor = [UIColor colorWithRed:0x5d / 255.0 green:0x5d / 255.0 blue:0x5d / 255.0 alpha:1.0];
        [self addSubview:firstLab];
        
        _secondBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _secondBtn.tag = 1;
        _secondBtn.exclusiveTouch = YES;
        _secondBtn.frame = CGRectOffset(iconFrame, offset, 0);
        [_secondBtn setImage:[UIImage imageNamed:@"icon_meizhuang"] forState:UIControlStateNormal];
        [_secondBtn addTarget:self action:@selector(onTapBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_secondBtn];
        
        UILabel* secondLab = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(_secondBtn.frame), CGRectGetMaxY(_secondBtn.frame), CGRectGetWidth(iconFrame), 10)];
        secondLab.backgroundColor = [UIColor clearColor];
        secondLab.text = @"美妆";
        secondLab.textAlignment = NSTextAlignmentCenter;
        secondLab.font = [UIFont systemFontOfSize:12];
        secondLab.textColor = [UIColor colorWithRed:0x5d / 255.0 green:0x5d / 255.0 blue:0x5d / 255.0 alpha:1.0];
        [self addSubview:secondLab];
        
        _thirdBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _thirdBtn.tag = 2;
        _thirdBtn.exclusiveTouch = YES;
        _thirdBtn.frame = CGRectOffset(iconFrame, offset * 2, 0);
        [_thirdBtn setImage:[UIImage imageNamed:@"icon_shuma"] forState:UIControlStateNormal];
        [_thirdBtn addTarget:self action:@selector(onTapBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_thirdBtn];
        
        UILabel* thirdLab = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(_thirdBtn.frame), CGRectGetMaxY(_thirdBtn.frame), CGRectGetWidth(iconFrame), 10)];
        thirdLab.backgroundColor = [UIColor clearColor];
        thirdLab.text = @"数码";
        thirdLab.textAlignment = NSTextAlignmentCenter;
        thirdLab.font = [UIFont systemFontOfSize:12];
        thirdLab.textColor = [UIColor colorWithRed:0x5d / 255.0 green:0x5d / 255.0 blue:0x5d / 255.0 alpha:1.0];
        [self addSubview:thirdLab];
        
        _moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _moreBtn.tag = 3;
        _moreBtn.exclusiveTouch = YES;
        _moreBtn.frame = CGRectOffset(iconFrame, offset * 3, 0);
        [_moreBtn setImage:[UIImage imageNamed:@"icon_more"] forState:UIControlStateNormal];
        [_moreBtn addTarget:self action:@selector(onTapBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_moreBtn];
        
        UILabel* moreLab = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(_moreBtn.frame), CGRectGetMaxY(_moreBtn.frame), CGRectGetWidth(iconFrame), 10)];
        moreLab.backgroundColor = [UIColor clearColor];
        moreLab.text = @"全部";
        moreLab.textAlignment = NSTextAlignmentCenter;
        moreLab.font = [UIFont systemFontOfSize:12];
        moreLab.textColor = [UIColor colorWithRed:0x5d / 255.0 green:0x5d / 255.0 blue:0x5d / 255.0 alpha:1.0];
        [self addSubview:moreLab];
        
        UIView* line = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(frame) - 1, CGRectGetWidth(frame), 1)];
        line.backgroundColor = [UIColor colorWithRed:0xf7 / 255.0 green:0xfa / 255.0 blue:0xfa / 255.0 alpha:1.0];
        [self addSubview:line];
    }
    
    return self;
}

- (void)clearScrollViewSubviews {
    for (UIView* subview in self.scrollView.subviews) {
        [subview removeFromSuperview];
    }
}

#pragma mark - 
- (void)onTapBtn:(UIButton*)btn {
    NSString* str = @"女装";
    if (1 == btn.tag) {
        str = @"美妆";
    } else if (2 == btn.tag) {
        str = @"数码";
    } else if (3 == btn.tag) {
        str = @"全部";
    }
    
    if (self.tapBtnHandler) {
        self.tapBtnHandler(str);
    }
}

- (void)onTapPageControl:(UIPageControl*)pageCtrl {
    [self.scrollView setContentOffset:CGPointMake(CGRectGetWidth(self.scrollView.frame) * pageCtrl.currentPage, 0) animated:YES];
}

@end
